<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px" size="small">
    <el-form-item label="计量器类型" prop="meterType">
      <el-select v-model="dataForm.meterType"  style="width: 100px;" placeholder="请选择" clearable @change="changeMeter">
        <el-option
          v-for="(item, index) in meterTypeList" :key="index"
          :label="item.paramValue"
          :value="item.paramKey">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="采集器编码" prop="coding">
      <el-input v-model="dataForm.coding" placeholder="采集器编码"></el-input>
    </el-form-item>
    <el-form-item label="本地系统标识" prop="collectType">
      <el-select v-model="dataForm.collectType"  style="width: 100%;" placeholder="请选择"  >
        <el-option
          v-for="(item, index) in collectTypeList" :key="index"
          :label="item.collectName"
          :value="item.collectType">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="原始采集项" prop="computeType">
      <el-radio-group v-model="dataForm.computeType">
        <el-radio label="0">原始采集项</el-radio>
        <el-radio label="1">计算采集项</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="原始采集输入项" prop="computeVar" v-if="dataForm.computeType==1">
      <el-input v-model="dataForm.computeVar" placeholder="原始采集输入项coding  多个用英文;进行分割"></el-input>
    </el-form-item>
    <el-form-item label="计算表达式" prop="computeVal" v-if="dataForm.computeType==1">
      <el-input v-model="dataForm.computeVal" placeholder="计算表达式  对输入的coding进行计算表达式"></el-input>
    </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="dataForm.notes" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false" size="small">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" size="small">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        meterTypeList:[],
        collectTypeList:[],
        visible: false,
        dataForm: {
          id: 0,
          meterType: '',
          coding: '',
          collectType: '',
          notes: '',
          computeVal: '',
          computeVar: '',
          computeType: '0'
        },
        dataRule: {
          meterType: [
            { required: true, message: '计量器类型不能为空', trigger: 'blur' }
          ],
          coding: [
            { required: true, message: '采集器编码不能为空', trigger: 'blur' }
          ],
          collectType: [
            { required: true, message: '本地系统标识不能为空', trigger: 'blur' }
          ],
          notes: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          computeVal: [
            { required: true, message: '计算表达式  对输入的coding进行计算表达式不能为空', trigger: 'blur' }
          ],
          computeVar: [
            { required: true, message: '原始采集输入项coding  多个用英文;进行分割不能为空', trigger: 'blur' }
          ],
          computeType: [
            { required: true, message: '原始采集项  0原始采集项  1计算采集项不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id,meterTypeList) {
        this.meterTypeList =meterTypeList
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$api.okhttp({
              url: this.$http.adornUrl(`/ecs/ecscollectitemcoding/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then((data) => {
              if (data && data.code === 0) {
                this.dataForm.meterType = data.ecscollectitemcoding.meterType
                this.dataForm.coding = data.ecscollectitemcoding.coding
                this.dataForm.collectType = data.ecscollectitemcoding.collectType
                this.dataForm.notes = data.ecscollectitemcoding.notes
                this.dataForm.computeVal = data.ecscollectitemcoding.computeVal
                this.dataForm.computeVar = data.ecscollectitemcoding.computeVar
                this.dataForm.computeType = data.ecscollectitemcoding.computeType
              }
            })
          }
          this.changeMeter()
        })
      },
      changeMeter(){
        let params = {
          'meterType':this.dataForm.meterType,
          "customerId": this.$util.cookies.get('customer_id'),
          'page': 1,
          'limit': 10000,
        }
        this.$api.okhttp({
          url: this.$http.adornUrl('/ecs/ecscollectitem/list'),
          method: 'get',
          params: this.$http.adornParams(params)
        }).then((data) => {
          if (data && data.code === 0) {
          this.collectTypeList = data.page.list
        } else {
          this.collectTypeList = []
        }
      });

      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$api.okhttp({
              url: this.$http.adornUrl(`/ecs/ecscollectitemcoding/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'meterType': this.dataForm.meterType,
                'coding': this.dataForm.coding,
                'collectType': this.dataForm.collectType,
                'notes': this.dataForm.notes,
                'computeVal': this.dataForm.computeVal,
                'computeVar': this.dataForm.computeVar,
                'computeType': this.dataForm.computeType,
                "customerId": this.$util.cookies.get('customer_id'),

              })
            }).then((data) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
